package com.zb.controller;

import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.zb.config.MQConfig;
import com.zb.service.PayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>添加描述信息</p>
 *
 * @author 李柯
 * @since 2022年11月10日 15:43
 */
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    @Autowired
    private Connection connection;

    @GetMapping("/create")
    public Map<String, String> create(String orderId, Integer money,String flag) {
        String username = "admin1";//换成认证框架
        return payService.create(orderId, money,flag,username);
    }

    @GetMapping("/query")
    public Map<String, String> query(String orderId) {
        return payService.query(orderId);
    }


    @GetMapping("/say")
    public String say(String name){
        System.out.println(name);
        return "hello:"+name;
    }

    @PostMapping("/notifyurl")
    public String notifyurl(HttpServletRequest request) {
        System.out.println("回调地址");
        try {
            InputStream is = request.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] data = new byte[1024];
            int len = 0;
            while ((len = is.read(data)) != -1) {
                baos.write(data, 0, len);
            }
            baos.close();
            is.close();
            String xml = new String(baos.toByteArray(), "UTF-8");
            Map<String, String> dataMap = WXPayUtil.xmlToMap(xml);
            String json=JSON.toJSONString(dataMap);
            String attachJSON = dataMap.get("attach");
            Map<String,String> attachMap = JSON.parseObject(attachJSON, Map.class);
            if (attachMap.get("flag").equals("1")){//普通订单
                System.out.println("普通订单=======");
                sendPaySuccessMsg(json);//向MQ发送数据
            }else {
                System.out.println("活动订单=======");
                sendSeckillSuccessMsg(json);//向MQ发送数据
            }
            System.out.println("获取的数据:" + xml);
            Map<String, String> returnVal = new HashMap<>();
            returnVal.put("return_code", "SUCCESS");
            returnVal.put("return_msg", "OK");
            return WXPayUtil.mapToXml(returnVal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendPaySuccessMsg(String json) throws Exception {
        rabbitTemplate.convertAndSend(MQConfig.PAY_EXCHANGE, MQConfig.PAY_ROUTING, json);

//        //创建通道
//        Channel channel = connection.createChannel();
//        //声明队列
//        channel.queueDeclare("orderPay", true, false, false, null);
//        //发送数据
//        channel.basicPublish("", "orderPay", null, json.getBytes());
    }

    public void sendSeckillSuccessMsg(String json) throws Exception {
        rabbitTemplate.convertAndSend(MQConfig.SECKILL_EXCHANGE, MQConfig.SECKILL_ROUTING, json);
    }
}
